package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController()
@Slf4j
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result list(String name, Integer subject, Integer target,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        PageResult<Course> pageResult = courseService.list(name, subject, target, page, pageSize);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        courseService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    public Result addCourse(@RequestBody Course course){
        courseService.addCourse(course);
        return  Result.success();
    }

    @GetMapping ("/{id}")
    public Result getById(@PathVariable Integer id){
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    @PutMapping
    public Result update(@RequestBody Course course){
        courseService.update(course);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(){
        return Result.success(courseService.listAll());
    }

    @GetMapping("/subject/{subject}")
    public Result getBySub(@PathVariable Integer subject){
        List<Course>  course = courseService.setBySub(subject);
        return Result.success(course);
    }
}
